package hiresoft.ui;

import hiresoft.network.*;
import hiresoft.users.Reviewer;
import javax.swing.*;

/**
 * Creates the User Interface and
 *  functionality for Reviewer accounts
 *
 * @author Kevin Carlino
 */
public class ReviewerFrame extends UserFrame {
    DefaultListModel model;
    int appID[];
    Network net;

    public ReviewerFrame(LoginFrame context, Reviewer _user){
        super(context, _user);
        initComponents();
    }

    /**
     * Sets the network/connection and calls the main function.
     */
    public void connect() {
        try {
        net = Network.getInstance();
            getApps();
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "No network connection established.");
        }
    }

    /**
     * Retrieves a list of all Applications in the database and displays them.
     */
     public void getApps() {
        model = ((DefaultListModel)lstPostings.getModel());
        try {
            net.out.Send(Packets.P_GETREVIEWERALLAPPS);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_GETREVIEWERALLAPPS)
                {
                        if(net.in.getDWord() != 0)
                        {
                            int appNums = net.in.getDWord();
                            appID = new int[appNums];

                            for(int i = 0; i < appNums; i++)
                            {
                                appID[i] = net.in.getDWord();
                                model.addElement(net.in.getString());
                            }
                        }
                        else
                            JOptionPane.showMessageDialog(null, "Failed to retrieve application listing.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
    }

     /**
      * Retrieves specific application information and displays it.
      */
     public void getApplication() {
       int selAppID = lstPostings.getSelectedIndex();

        try {
            net.out.Insert(appID[selAppID]);
            net.out.Send(Packets.P_GETREVIEWERAPP);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_GETREVIEWERAPP)
                {
                    if(net.in.getDWord() != 0)
                    {
                        int jobID = net.in.getDWord();
                        String jobTitle = net.in.getString();
                        String applicantName = net.in.getString();
                        String applicantResume = net.in.getString();
                        String referenceName1 = net.in.getString();
                        String referenceMessage1 = net.in.getString();
                        String referenceName2 = net.in.getString();
                        String referenceMessage2 = net.in.getString();
                        String referenceName3 = net.in.getString();
                        String referenceMessage3 = net.in.getString();

                        lblJobID.setText(String.valueOf(jobID));
                        lblJobTitle.setText(jobTitle);
                        lblApplicantName.setText(applicantName);
                        txtResume.setText(applicantResume);
                        lblReference1.setText(referenceName1);
                        txtReference1.setText(referenceMessage1);
                        lblReference2.setText(referenceName2);
                        txtReference2.setText(referenceMessage2);
                        lblReference3.setText(referenceName3);
                        txtReference3.setText(referenceMessage3);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Failed to retrieve job information.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
       getRating(appID[selAppID]);
    }

     /**
      * Retrieves specific application rating and displays it.
      */
     public void getRating(int appID) {
        try {
            net.out.Insert(appID);
            net.out.Send(Packets.P_GETRATING);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_GETRATING)
                {
                    if(net.in.getDWord() != 0)
                    {
                        int rating = net.in.getDWord();
                        String comment = net.in.getString();

                        sldrRating.setValue(rating);
                        txtComments.setText(comment);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Failed to retrieve rating information.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
    }

    /**
     * Sends application rating given by user.
     */
    public void sendRating() {
       int selAppID = lstPostings.getSelectedIndex();

        try {
            net.out.Insert(appID[selAppID]);
            net.out.Insert(sldrRating.getValue());
            net.out.Insert(txtComments.getText());
            net.out.Send(Packets.P_SENDRATING);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_SENDRATING)
                {
                    if(net.in.getDWord() != 0)
                        JOptionPane.showMessageDialog(null, "Successfully sent rating information.");
                    else
                        JOptionPane.showMessageDialog(null, "Failed to send rating information.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtReference2 = new javax.swing.JTextArea();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtReference3 = new javax.swing.JTextArea();
        lblReference3 = new javax.swing.JLabel();
        jScrollPane6 = new javax.swing.JScrollPane();
        txtComments = new javax.swing.JTextArea();
        lblReference1 = new javax.swing.JLabel();
        lblReference2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtReference1 = new javax.swing.JTextArea();
        lblComments = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstPostings = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtResume = new javax.swing.JTextArea();
        lblApplicantName = new javax.swing.JLabel();
        lblJobTitle = new javax.swing.JLabel();
        lblJobID = new javax.swing.JLabel();
        lblResume = new javax.swing.JLabel();
        sldrRating = new javax.swing.JSlider();
        lblRating = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        txtReference2.setColumns(20);
        txtReference2.setEditable(false);
        txtReference2.setRows(5);
        txtReference2.setText("Reference2's input");
        jScrollPane4.setViewportView(txtReference2);

        txtReference3.setColumns(20);
        txtReference3.setEditable(false);
        txtReference3.setRows(5);
        txtReference3.setText("Reference3's input");
        jScrollPane5.setViewportView(txtReference3);

        lblReference3.setText("Reference 3");

        txtComments.setColumns(20);
        txtComments.setRows(5);
        txtComments.setText("Your Comments");
        jScrollPane6.setViewportView(txtComments);

        lblReference1.setText("Reference 1");

        lblReference2.setText("Reference 2");

        txtReference1.setColumns(20);
        txtReference1.setEditable(false);
        txtReference1.setRows(5);
        txtReference1.setText("Reference1's input");
        jScrollPane3.setViewportView(txtReference1);

        lblComments.setText("Comments");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                    .addComponent(lblReference1)
                    .addComponent(lblReference2)
                    .addComponent(lblReference3)
                    .addComponent(lblComments)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblReference1)
                .addGap(2, 2, 2)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblReference2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblReference3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblComments)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
        );

        lstPostings.setModel(new DefaultListModel());
        lstPostings.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstPostingsMouseClicked(evt);
            }
        });
        lstPostings.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstPostingsValueChanged(evt);
            }
        });
        lstPostings.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                lstPostingsKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(lstPostings);

        txtResume.setColumns(20);
        txtResume.setEditable(false);
        txtResume.setRows(5);
        jScrollPane1.setViewportView(txtResume);

        lblApplicantName.setText("Applicant Name");

        lblJobTitle.setText("Job Title");

        lblJobID.setText("Posting ID");

        lblResume.setText("Resume");

        sldrRating.setMajorTickSpacing(25);
        sldrRating.setMinorTickSpacing(5);
        sldrRating.setPaintLabels(true);
        sldrRating.setPaintTicks(true);
        sldrRating.setSnapToTicks(true);
        sldrRating.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                sldrRatingCaretPositionChanged(evt);
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            }
        });

        lblRating.setText("Rating:");

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lblJobTitle)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
                                .addComponent(lblJobID))
                            .addComponent(lblResume)
                            .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lblRating)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sldrRating, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                                .addGap(35, 35, 35))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addComponent(lblApplicantName)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblApplicantName)
                        .addGap(96, 96, 96)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblJobID)
                            .addComponent(lblJobTitle))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(lblResume)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblRating)
                                    .addComponent(sldrRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstPostingsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lstPostingsKeyReleased

    }//GEN-LAST:event_lstPostingsKeyReleased

    private void lstPostingsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstPostingsMouseClicked
        
    }//GEN-LAST:event_lstPostingsMouseClicked

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        sendRating();
    }//GEN-LAST:event_btnSaveActionPerformed

    private void sldrRatingCaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_sldrRatingCaretPositionChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_sldrRatingCaretPositionChanged

    private void lstPostingsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstPostingsValueChanged
        // TODO add your handling code here:
        getApplication();
    }//GEN-LAST:event_lstPostingsValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblApplicantName;
    private javax.swing.JLabel lblComments;
    private javax.swing.JLabel lblJobID;
    private javax.swing.JLabel lblJobTitle;
    private javax.swing.JLabel lblRating;
    private javax.swing.JLabel lblReference1;
    private javax.swing.JLabel lblReference2;
    private javax.swing.JLabel lblReference3;
    private javax.swing.JLabel lblResume;
    private javax.swing.JList lstPostings;
    private javax.swing.JSlider sldrRating;
    private javax.swing.JTextArea txtComments;
    private javax.swing.JTextArea txtReference1;
    private javax.swing.JTextArea txtReference2;
    private javax.swing.JTextArea txtReference3;
    private javax.swing.JTextArea txtResume;
    // End of variables declaration//GEN-END:variables

}
